# coding=utf-8

from selenium import webdriver
from pyvirtualdisplay import Display
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

chrome_options = Options()
#加上下面两行，解决报错 禁止使用sandbox
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

# 初始化屏幕
display = Display(visible=0, size=(800, 600))
display.start()
#打开浏览器
driver = webdriver.Chrome(executable_path="/usr/local/chromedriver/chromedriver", chrome_options=chrome_options)
print driver.title
# driver = webdriver.PhantomJS()
driver.get("http://www.baidu.com")
print driver.title
data = driver.find_element_by_id("wrapper").text
# print data

driver.save_screenshot("/usr/local/pythonwork/baidu.png")

# 搜索关键字
driver.find_element_by_id("kw").send_keys(u"方友运")
driver.save_screenshot("/usr/local/pythonwork/fyy.png")

# 搜索
# driver.find_element_by_id("su").click()
driver.find_element_by_id("su").send_keys(Keys.RETURN)

print(driver.page_source)